package edu.gd.servlet;

import com.google.gson.Gson;
import edu.gd.dao.UserDao;
import edu.gd.entity.BaseResponse;
import edu.gd.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/user/all/")
public class UserallServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> users=UserDao.getUsers();
        resp.setContentType("application/json;character=utf-8");
        BaseResponse response=new BaseResponse();
        response.setCode(200);
        response.setMsg("请求成功");
        response.setData(users);
        Gson gson=new Gson();
        String json=gson.toJson(response);
        PrintWriter out=resp.getWriter();
        out.write(json);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
